#coding:utf-8
"""Copyright (c) 2022 God Forever
Alarm Clock is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details."""
import random
import os
import datetime
import time
try:
    all_songs = []
    all_songs_dict = {}
    for root, dirs, names in os.walk("d:/Users/{}/Music".format(os.getlogin())):
        for filename in names:
            if os.path.join(root, filename)[-4:] == ".mp3" or os.path.join(root, filename)[-4:] == ".ogg":
                all_songs.append(os.path.join(root, filename)[os.path.join(root, filename).find("\\") + 1:-4])
                all_songs_dict[
                    os.path.join(root, filename)[os.path.join(root, filename).find("\\") + 1:-4]] = os.path.join(root,
                                                                                                                 filename).replace(
                    "\\", "/")
    finding = open("alarm_list.txt", "a")
    finding.close()
    finding = open("alarm_list.txt", "r")
    alarm_list_undone = finding.readlines()
    finding.close()
    alarm_list = []
    for i in alarm_list_undone:
        alarm_list.append(i.replace("\n", ""))
    finding = open("dates_one.txt", "a")
    finding.close()
    finding = open("dates_one.txt", "r")
    dates_undone_one = finding.readlines()
    finding.close()
    finding = open("dates_two.txt", "a")
    finding.close()
    finding = open("dates_two.txt", "r")
    dates_undone_two = finding.readlines()
    finding.close()
    dates = {}
    for i in range(len(dates_undone_one)):
        dates[dates_undone_one[i].replace("\n", "")] = eval(dates_undone_two[i].replace("\n", ""))
    finding = open("songs_one.txt", "a")
    finding.close()
    finding = open("songs_one.txt", "r")
    songs_undone_one = finding.readlines()
    finding.close()
    finding = open("songs_two.txt", "a")
    finding.close()
    finding = open("songs_two.txt", "r")
    songs_undone_two = finding.readlines()
    finding.close()
    songs = {}
    for i in range(len(songs_undone_two)):
        try:
            open(songs_undone_two[i].replace("\n", ""), "r")
        except:
            songs_undone_two[i] = all_songs_dict[all_songs[random.randint(0, len(all_songs) - 1)]] + "\n"
            with open("songs_two.txt", "w") as finding:
                finding.writelines(songs_undone_two)
    for i in range(len(songs_undone_one)):
        songs[songs_undone_one[i].replace("\n", "")] = songs_undone_two[i].replace("\n", "")
    finding = open("alarm_list.txt", "r")
    finder = finding.read()
    try:
        while finder[-1] == "\n":
            finder = finder[:-1]
    except:
        while False:
            print()
    with open("alarm_list.txt", "w") as finding:
        finding.write(finder)
    finding = open("dates_one.txt", "r")
    finder = finding.read()
    try:
        while finder[-1] == "\n":
            finder = finder[:-1]
    except:
        while False:
            print()
    with open("dates_one.txt", "w") as finding:
        finding.write(finder)
    finding = open("dates_two.txt", "r")
    finder = finding.read()
    try:
        while finder[-1] == "\n":
            finder = finder[:-1]
    except:
        while False:
            print()
    with open("dates_two.txt", "w") as finding:
        finding.write(finder)

    finding = open("songs_one.txt", "r")
    finder = finding.read()
    try:
        while finder[-1] == "\n":
            finder = finder[:-1]
    except:
        while False:
            print()
    with open("songs_one.txt", "w") as finding:
        finding.write(finder)
    finding = open("songs_two.txt", "r")
    finder = finding.read()
    try:
        while finder[-1] == "\n":
            finder = finder[:-1]
    except:
        while False:
            print()
    with open("songs_two.txt", "w") as finding:
        finding.write(finder)
    print("请选择服务：")
    print()
    print("1:新建闹钟")
    print("2:管理闹钟")
    print()
    top = input("你的选择：")
    if top == "1":
        print()
        set_time = input(" 请输入闹钟时间（格式：00:00）：")
        have_it = False
        for i in alarm_list:
            if i == set_time:
                have_it = True
        try:
            alarm_list_undone[-1] += "\n"
        except:
            if False:
                print()
        alarm_list_undone.append(set_time)
        alarm_list = []
        for i in alarm_list_undone:
            alarm_list.append(i.replace("\n", ""))
        with open("alarm_list.txt", "w") as finding:
            finding.writelines(alarm_list_undone)
        print()
        print(" 请选择响铃次数：")
        print()
        print(" 1:仅一次")
        print(" 2:每天")
        print(" 3:工作日")
        print(" 4:自定义")
        print()
        set_date = input(" 你的选择：")
        if set_date == "1":
            if int(set_time[:set_time.find(":")]) * 60 + int(set_time[set_time.find(":") + 1:]) <= int(
                    str(datetime.datetime.now())[11:16][:str(datetime.datetime.now())[11:16].find(":")]) * 60 + int(
                    str(datetime.datetime.now())[11:16][str(datetime.datetime.now())[11:16].find(":") + 1:]):
                try:
                    dates_undone_one[-1] += "\n"
                except:
                    if False:
                        print()
                dates_undone_one.append(set_time)
                try:
                    dates_undone_two[-1] += "\n"
                except:
                    if False:
                        print()
                dates_undone_two.append(str([(datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%w"),
                                             "kill{}&{}".format(
                                                 (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y"),
                                                 (datetime.datetime.now() + datetime.timedelta(days=1)).strftime(
                                                     "%j"))]))
            else:
                try:
                    dates_undone_one[-1] += "\n"
                except:
                    if False:
                        print()
                dates_undone_one.append(set_time)
                try:
                    dates_undone_two[-1] += "\n"
                except:
                    if False:
                        print()
                dates_undone_two.append(str([(datetime.datetime.now()).strftime("%w"),
                                             "kill{}&{}".format((datetime.datetime.now()).strftime("%Y"),
                                                                (datetime.datetime.now()).strftime("%j"))]))

            dates = {}
            for i in range(len(dates_undone_one)):
                dates[dates_undone_one[i].replace("\n", "")] = eval(dates_undone_two[i].replace("\n", ""))
            with open("dates_one.txt", "w") as finding:
                finding.writelines(dates_undone_one)
            with open("dates_two.txt", "w") as finding:
                finding.writelines(dates_undone_two)
        if set_date == "2":
            try:
                dates_undone_one[-1] += "\n"
            except:
                if False:
                    print()
            dates_undone_one.append(set_time)
            try:
                dates_undone_two[-1] += "\n"
            except:
                if False:
                    print()
            dates_undone_two.append(str(["1", "2", "3", "4", "5", "6", "0", "kill9999&99"]))
            dates = {}
            for i in range(len(dates_undone_one)):
                dates[dates_undone_one[i].replace("\n", "")] = eval(dates_undone_two[i].replace("\n", ""))
            with open("dates_one.txt", "w") as finding:
                finding.writelines(dates_undone_one)
            with open("dates_two.txt", "w") as finding:
                finding.writelines(dates_undone_two)
        if set_date == "3":
            try:
                dates_undone_one[-1] += "\n"
            except:
                if False:
                    print()
            dates_undone_one.append(set_time)
            try:
                dates_undone_two[-1] += "\n"
            except:
                if False:
                    print()
            dates_undone_two.append(str(["1", "2", "3", "4", "5", "kill9999&99"]))
            dates = {}
            for i in range(len(dates_undone_one)):
                dates[dates_undone_one[i].replace("\n", "")] = eval(dates_undone_two[i].replace("\n", ""))
            with open("dates_one.txt", "w") as finding:
                finding.writelines(dates_undone_one)
            with open("dates_two.txt", "w") as finding:
                finding.writelines(dates_undone_two)
        if set_date == "4":
            list_one = []
            math_to_week = {0: "星期日", 1: "星期一", 2: "星期二", 3: "星期三", 4: "星期四", 5: "星期五", 6: "星期六"}
            for i in range(7):
                print()
                yes_no = input(" {}是否响铃（是输入“1”，否输入“0”）：".format(math_to_week.get(i)))
                if yes_no == "1":
                    list_one.append(str(i))
            list_one.append("kill9999&99")
            try:
                dates_undone_one[-1] += "\n"
            except:
                if False:
                    print()
            dates_undone_one.append(set_time)
            try:
                dates_undone_two[-1] += "\n"
            except:
                if False:
                    print()
            dates_undone_two.append(str(list_one))
            dates = {}
            for i in range(len(dates_undone_one)):
                dates[dates_undone_one[i].replace("\n", "")] = eval(dates_undone_two[i].replace("\n", ""))
            with open("dates_one.txt", "w") as finding:
                finding.writelines(dates_undone_one)
            with open("dates_two.txt", "w") as finding:
                finding.writelines(dates_undone_two)
        print()
        print(" 请选择铃声：")
        print()
        num = 0
        for i in all_songs:
            num += 1
            print(" " + str(num) + ":《" + i + "》")
        num += 1
        print(" " + str(num) + ":自选")
        print()
        set_song = input(" 你的选择：")
        if not set_song == str(num):
            song = all_songs_dict[all_songs[int(set_song) - 1]]
        else:
            print()
            song = input(" 请输入路径：")
        try:
            songs_undone_one[-1] += "\n"
        except:
            if False:
                print()
        songs_undone_one.append(set_time)
        try:
            songs_undone_two[-1] += "\n"
        except:
            if False:
                print()
        songs_undone_two.append(song)
        songs = {}
        for i in range(len(songs_undone_two)):
            try:
                open(songs_undone_two[i].replace("\n", ""), "r")
            except:
                songs_undone_two[i] = all_songs_dict[all_songs[random.randint(0, len(all_songs) - 1)]] + "\n"
                with open("songs_two.txt", "w") as finding:
                    finding.writelines(songs_undone_two)
        for i in range(len(songs_undone_one)):
            songs[songs_undone_one[i].replace("\n", "")] = songs_undone_two[i].replace("\n", "")
        with open("songs_one.txt", "w") as finding:
            finding.writelines(songs_undone_one)
        with open("songs_two.txt", "w") as finding:
            finding.writelines(songs_undone_two)
    else:
        print()
        print(" 请选择需要删除的闹钟：")
        print()
        for i in range(len(alarm_list)):
            if dates[alarm_list[i]][-1] != "kill9999&99":
                date_type = "仅一次"
            elif dates[alarm_list[i]] == ["1", "2", "3", "4", "5", "6", "0", "kill9999&99"]:
                date_type = "每天"
            elif dates[alarm_list[i]] == ["1", "2", "3", "4", "5", "kill9999&99"]:
                date_type = "工作日"
            else:
                date_type = "自定义"
            print(" {}:时间：{}；响铃次数：{}；音乐：《{}》；".format(i + 1, alarm_list[i], date_type, songs[alarm_list[i]][
                                                                                      songs[alarm_list[i]].rfind(
                                                                                          "/") + 1:songs[
                                                                                          alarm_list[i]].rfind(".")]))
        print()
        delete = int(input(" 你的选择："))
        alarm_list_undone.pop(delete - 1)
        dates_undone_one.pop(delete - 1)
        dates_undone_two.pop(delete - 1)
        songs_undone_one.pop(delete - 1)
        songs_undone_two.pop(delete - 1)
        with open("alarm_list.txt", "w") as finding:
            finding.writelines(alarm_list_undone)
        with open("songs_one.txt", "w") as finding:
            finding.writelines(songs_undone_one)
        with open("songs_two.txt", "w") as finding:
            finding.writelines(songs_undone_two)
        with open("dates_one.txt", "w") as finding:
            finding.writelines(dates_undone_one)
        with open("dates_two.txt", "w") as finding:
            finding.writelines(dates_undone_two)
        finding = open("alarm_list.txt", "a")
        finding.close()
        finding = open("alarm_list.txt", "r")
        alarm_list_undone = finding.readlines()
        finding.close()
        alarm_list = []
        for i in alarm_list_undone:
            alarm_list.append(i.replace("\n", ""))
        finding = open("dates_one.txt", "a")
        finding.close()
        finding = open("dates_one.txt", "r")
        dates_undone_one = finding.readlines()
        finding.close()
        finding = open("dates_two.txt", "a")
        finding.close()
        finding = open("dates_two.txt", "r")
        dates_undone_two = finding.readlines()
        finding.close()
        dates = {}
        for i in range(len(dates_undone_one)):
            dates[dates_undone_one[i].replace("\n", "")] = eval(dates_undone_two[i].replace("\n", ""))
        finding = open("songs_one.txt", "a")
        finding.close()
        finding = open("songs_one.txt", "r")
        songs_undone_one = finding.readlines()
        finding.close()
        finding = open("songs_two.txt", "a")
        finding.close()
        finding = open("songs_two.txt", "r")
        songs_undone_two = finding.readlines()
        finding.close()
        songs = {}
        for i in range(len(songs_undone_two)):
            try:
                open(songs_undone_two[i].replace("\n", ""), "r")
            except:
                songs_undone_two[i] = all_songs_dict[all_songs[random.randint(0, len(all_songs) - 1)]] + "\n"
                with open("songs_two.txt", "w") as finding:
                    finding.writelines(songs_undone_two)
        for i in range(len(songs_undone_one)):
            songs[songs_undone_one[i].replace("\n", "")] = songs_undone_two[i].replace("\n", "")
        finding = open("alarm_list.txt", "r")
        finder = finding.read()
        try:
            while finder[-1] == "\n":
                finder = finder[:-1]
        except:
            while False:
                print()
        with open("alarm_list.txt", "w") as finding:
            finding.write(finder)
        finding = open("dates_one.txt", "r")
        finder = finding.read()
        try:
            while finder[-1] == "\n":
                finder = finder[:-1]
        except:
            while False:
                print()
        with open("dates_one.txt", "w") as finding:
            finding.write(finder)
        finding = open("dates_two.txt", "r")
        finder = finding.read()
        try:
            while finder[-1] == "\n":
                finder = finder[:-1]
        except:
            while False:
                print()
        with open("dates_two.txt", "w") as finding:
            finding.write(finder)

        finding = open("songs_one.txt", "r")
        finder = finding.read()
        try:
            while finder[-1] == "\n":
                finder = finder[:-1]
        except:
            while False:
                print()
        with open("songs_one.txt", "w") as finding:
            finding.write(finder)
        finding = open("songs_two.txt", "r")
        finder = finding.read()
        try:
            while finder[-1] == "\n":
                finder = finder[:-1]
        except:
            while False:
                print()
        with open("songs_two.txt", "w") as finding:
            finding.write(finder)
except:
    while False:
        print()